package com.mall.web.util;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;

import com.mall.common.util.RandomUtil;
import com.mall.constant.ReqAttributeKey;
import com.mall.pojo.User;

/**
 * 
 * @author is_zhoufeng@163.com 2015年1月25日 下午5:06:04
 */
public class WebUtil {

	static final Logger log = org.slf4j.LoggerFactory.getLogger(WebUtil.class);

	/**
	 * 获取当前登录用户,如果没有登录，返回null
	 * 
	 * @param request
	 * @return
	 */
	public static User getCurrentUser(HttpServletRequest request) {
		User user = null;
		Object userObj = request.getAttribute(ReqAttributeKey.CURRENT_LOGIN_USER);
		if (userObj == null) {
			userObj = request.getSession().getAttribute(ReqAttributeKey.CURRENT_LOGIN_USER);
		}
		if (userObj != null && userObj instanceof User) {
			user = (User) userObj;
		}
		return user;
	}

	/**
	 * 获取sessionId
	 * 
	 * @param request
	 * @return
	 */
	public static String getSessionId(HttpServletRequest request) {
		return request.getSession().getId();
	}

	/**
	 * 获取到cookie里用户对象的value
	 * 
	 * @param request
	 * @return
	 */
	public static String getUserCookieValue(HttpServletRequest request) {
		String juid = CookieUtil.getCookieValue(request, ReqAttributeKey.COOKIE_FOR_USER);
		if (StringUtils.isBlank(juid)) {
			juid = getSessionId(request) + RandomUtil.generateString(4);
		}
		return juid;
	}

	/**
	 * 设置用户的cookie，用户登录,返回cookie的value
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	public static String setCurrentUserCookies(HttpServletRequest request, HttpServletResponse response) {
		String juid = getUserCookieValue(request);
		CookieUtil.setCookie(response, ReqAttributeKey.COOKIE_FOR_USER, juid);
		return juid;
	}

	/**
	 * session and cookie 设置用户信息
	 * 
	 * @param request
	 * @param response
	 * @param currentUser
	 */
	public static void setCurrentUserSessionAndCookies(HttpServletRequest request, HttpServletResponse response,
			User currentUser) {
		setCurrentUserCookies(request, response);
		request.setAttribute(ReqAttributeKey.CURRENT_LOGIN_USER, currentUser);
		request.getSession().setAttribute(ReqAttributeKey.CURRENT_LOGIN_USER, currentUser);
	}
	
	/**
	 * session and cookie 登录注册使用 设置用户信息
	 * 
	 * @param request
	 * @param response
	 * @param currentUser
	 */
	public static void setCurrentUserSessionAndCookiesFromLoginAndRegister(HttpServletRequest request, HttpServletResponse response,String juid,
			User currentUser) {
		CookieUtil.setCookie(response, ReqAttributeKey.COOKIE_FOR_USER, juid);
		request.setAttribute(ReqAttributeKey.CURRENT_LOGIN_USER, currentUser);
		request.getSession().setAttribute(ReqAttributeKey.CURRENT_LOGIN_USER, currentUser);
	}
	
	/**
	 * 设置commOnlyKey的cookie，用户登录,返回cookie的value
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	public static String setCurrentCommOnlyKeyCookies(HttpServletRequest request, HttpServletResponse response,Integer commOnlyKey) {
		CookieUtil.setCookie(response, ReqAttributeKey.COOKIE_FOR_COMMKEYID, commOnlyKey+"");
		return commOnlyKey+"";
	}

	/**
	 * session and cookie 设置commOnlyKey信息
	 * 
	 * @param request
	 * @param response
	 * @param currentUser
	 */
	public static void setCurrentCommOnlyKeySessionAndCookies(HttpServletRequest request, HttpServletResponse response,
			Integer commOnlyKey) {
		setCurrentCommOnlyKeyCookies(request, response,commOnlyKey);
		request.setAttribute(ReqAttributeKey.COOKIE_FOR_COMMKEYID, commOnlyKey+"");
		request.getSession().setAttribute(ReqAttributeKey.COOKIE_FOR_COMMKEYID, commOnlyKey+"");
	}

}
